﻿// C# 运算符和表达式

//var p1 = new Point(2, 3, "A");
//var p2 = new Point(1, 3, "B");
//var p3 = new Point(2, 3, "A");

//Console.WriteLine(p1 == p2);  // output: False
//Console.WriteLine(p1 == p3);  // output: True

//byte[] p4 = { 1, 2, 3 };

Payload pp1 = new byte[] { 1, 2, 3 };
Payload pp2 = new byte[] { 1, 2, 3 };

Console.WriteLine(pp1 == pp2);


Console.ReadKey();

public record Point(int X, int Y, string Name);

public class Payload : IEquatable<Payload>
{
    byte[] body;

    public Payload(byte[] body)
    {
        this.body = body;
    }

    public static implicit operator byte[](Payload payload) => payload.body;

    public static implicit operator Payload(byte[] payload) => new(payload);

    public static bool operator !=(Payload r1, Payload r2) => !(r1 == r2);

    public static bool operator ==(Payload r1, Payload r2) => r1.Equals(r2);

    public bool Equals(Payload? other)
    {
        if(other is null)
            return false;

        return this.body.SequenceEqual(other.body);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Payload);
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}